﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ToDoClear.HandleLogic;

namespace ToDoClear.Motion
{

    public class ScrollMotion:BaseMotion
    {
        private const string TAG = "ScrollMotion";
        public ToDoList todolist;

        //top bottom scroll, drag with speed
        public ScrollMotion(ToDoList _todolist, double VerticalVelocity)
            : base()
        {
            ToDoCanvas.getInstance().context.scrollMotion = this;
            todolist = _todolist;
            todolist.speed = getBestSpeed(VerticalVelocity);
            HandleManager.EnterState(MainState.Scroll);
            if (todolist.speed > 0)
            {
                HandleManager.EnterSubState(SubState.ScrollBottom);
            }
            else
            {
                HandleManager.EnterSubState(SubState.ScrollTop);
            }
        }
        //check over ,if over then scroll back
        public ScrollMotion(ToDoList _todolist)
            : base()
        {
            ToDoCanvas.getInstance().context.scrollMotion = this;
            todolist = _todolist;
            HandleManager.EnterState(MainState.Scroll);
            CheckOverList();
        }


        protected override void OnTick()
        {
            FpsDetect.Update();
            Log.d(TAG, "ontick");
            MainState mainState = ToDoCanvas.getInstance().context.state;
            SubState subState = ToDoCanvas.getInstance().context.substate;
             
            if (subState == SubState.ScrollTop || subState == SubState.ScrollBottom)
            {

                #region scroll

                if (todolist.speed == 0)
                {
                    #region check over list
                    CheckOverList();
                    todolist.ReSize();
                    return;
                    #endregion

                }
                else
                {
                    if (todolist.Offset > todolist.MaxScrollOffset())
                    {
                        //resume top
                        HandleManager.EnterSubState(SubState.TopOverResume);
                        todolist.speed = (-1) * ConstVar.ResumeSpeed;
                    }
                    else if (todolist.Offset < todolist.MinScrollOffset())
                    {
                        //resum bottom
                        HandleManager.EnterSubState(SubState.BottomOverResume);
                        todolist.speed = ConstVar.ResumeSpeed;
                    }
                    else
                    {
                        todolist.Offset += todolist.speed;
                        todolist.SlowSpeed();
                    }
                   
                }
               

                #endregion

            }
            else
            {
                #region  resume
                if (subState == SubState.BottomOverResume)
                {
                    if (todolist.Offset >= todolist.BottomStopOffset())
                    {
                        todolist.speed = 0;
                        todolist.Offset = todolist.BottomStopOffset();
                        HandleManager.EnterState(MainState.IdleState);
                        Stop();
                    }
                    else
                    {
                        todolist.Offset += todolist.speed;
                        if (todolist.Offset >= todolist.BottomStopOffset())
                        {
                            todolist.speed = 0;
                            todolist.Offset = todolist.BottomStopOffset();
                            HandleManager.EnterState(MainState.IdleState);
                            Stop();
                        }
                    }
                }
                else
                {
                    if (todolist.Offset <= todolist.TopStopOffset())
                    {
                        todolist.speed = 0;
                        todolist.Offset = todolist.TopStopOffset();
                        HandleManager.EnterState(MainState.IdleState);
                        Stop();
                    }
                    else
                    {
                        todolist.Offset += todolist.speed;
                        if (todolist.Offset <= todolist.TopStopOffset())
                        {
                            todolist.speed = 0;
                            todolist.Offset = todolist.TopStopOffset();
                            HandleManager.EnterState(MainState.IdleState);
                            Stop();
                        }
                    }
                }


                #endregion
            }

            todolist.ReSize();
           // if(todolist.speed

        }

        public void CheckOverList()
        {
            if (todolist.Offset > todolist.TopStopOffset())
            {
                //resume top
                HandleManager.EnterSubState(SubState.TopOverResume);
                todolist.speed = (-1) * ConstVar.ResumeSpeed;
            }
            else if (todolist.Offset < todolist.BottomStopOffset())
            {
                //resum bottom
                HandleManager.EnterSubState(SubState.BottomOverResume);
                todolist.speed = ConstVar.ResumeSpeed;
            }
            else
            {
                //stop
                HandleManager.EnterState(MainState.IdleState);
                Stop();
            }
        }
        public double getBestSpeed(double VerticalVelocity)
        {
            double speed = 0;
            if (Math.Abs(VerticalVelocity) < 500)
            {
                speed = 0;
            }
            else if (Math.Abs(VerticalVelocity) < 1000)
            {
                speed = 5;
            }
            else if (Math.Abs(VerticalVelocity) < 1500)
            {
                speed = 8;
            }
            else
            {
                speed = 11;
            }

            if (VerticalVelocity < 0)
                speed *= (-1);

            return speed;
        }
    }
}
